DAMN = (typeof DAMN != "undefined")?  DAMN : {};

DAMN.MetaData = new function() 
{   
  this.AttachTo = function(parent) {
    parent.tabs('add', '#tabs-MetaData', "MetaData");
    this.tab = $("#tabs-MetaData");
    
    DAMN.AssetMananger.Register("asset", this.MetaData.bind(this));
	};

  this.Refresh = function(id, target) 
	{
	  var acc = $('<div id="metadata-accordion"></div>');
	  acc.addClass('ui-accordion ui-widget ui-helper-reset ui-accordion-icons');
    target.append(acc);
    
    var general = new DAMN.MetaData.Header("General").AttachTo(acc);
    $.getJSON("/assets/"+id+"/info/", function(data){
      general.empty();    
      general.append($('<span>'+'id'+': '+data[0].pk+'</span><br/>'));
      data = data[0].fields;                      
      for(m in data){
        if (m == 'files'){
          for(f in data[m]){
            var file = data[m][f];
            d = file[1].substring(file[1].lastIndexOf('/')+1, file[1].length)
            general.append($('<span title="'+file+'">file: <a href="#TODO">'+d+'</a></span><br/>'));
          }
        }else {
          general.append($('<span title="'+data[m]+'">'+m+': '+data[m]+'</span><br/>'));
        }
      }
    });
    
    var dependencies = new DAMN.MetaData.Header("Dependencies").AttachTo(acc);
    $.getJSON("/assets/"+id+"/dependencies/", function(data){
      dependencies.empty();                              
      for(m in data){
        dependencies.append($('<span><a href="#" class="go-to-asset" asset="'+data[m].pk+'">'+data[m].fields.subName+'<a> ('+data[m].fields.format+')</span><br/>'));
      }
    });
    
    var metadata = new DAMN.MetaData.Header("MetaData").AttachTo(acc);
    $.getJSON("/assets/"+id+"/metadata/", function(data){
      metadata.empty();                              
      for(m in data){
        metadata.append($('<span>'+data[m].fields.name+': '+data[m].fields.value+'</span><br/>'));
      }
    });
  };
	
	this.MetaData = function(op, el) 
  { 
    this.tab.empty();
    if (op == 'SELECT') { 
      if (DAMN.AssetMananger.selected.length == 1) {
        this.Refresh(el.data("assetid"), this.tab);
      }
      else {
       this.tab.append("<p>Multiple selected."+DAMN.AssetMananger.selected.length+"</p>");
      }
    }
    else {
      this.tab.append("<p>Nothing selected.</p>");
    }
  };
  		
};

DAMN.MetaData.Header = function(title) {
  this.element = $("<div></div>");
  this.url = $('<a href="#" class="ui-accordion-header ui-helper-reset ui-state-default"></a>');
  this.url.text(title);
  this.url.append($('<span class="ui-icon ui-icon-triangle-1-s"></span>'));
  
  this.content = $('<div class="ui-widget ui-widget-content"></div>').hide();

  this.element.append(this.url);
  this.element.append(this.content);
  
  var self = this;
  this.url.toggle(
      function () {
        this.url.addClass("ui-state-active");
        this.content.show();
      }.bind(self),
      function () {
        this.url.removeClass("ui-state-active");
        this.content.hide();
      }.bind(self)
      );
  
  this.content.text("Loading...");
  
  this.AttachTo = function(parent) {
    parent.append(this.element); 
    return this;
	};
  this.empty = function() 
  { 
    this.content.empty();
  };
  
  this.append = function(el) 
  { 
    this.content.append(el);
  };
};
